﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ADC.DAL;

namespace ADC.BL
{
   public class AdvertisementFactory
    {
        public static List<Advertisement> GetAll()
        {
            var db = new dbADCEntities();
            return db.Advertisements.ToList();

        }

        public static int Delete(int advertisementID)
        {
            var db = new dbADCEntities();
            var advertisement = db.Advertisements.Where(g => g.AdvertisementID == advertisementID).FirstOrDefault();
            db.DeleteObject(advertisement);
            return db.SaveChanges();
        }

        public static Advertisement GetByID(int advertisementID)
        {
            var db = new dbADCEntities();
            return db.Advertisements.Where(g => g.AdvertisementID.Equals(advertisementID)).FirstOrDefault();
        }

        public static int Update(Advertisement advertisement)
        {
            var db = new dbADCEntities();
            var objAdvertisement = db.Advertisements.Where(g => g.AdvertisementID == advertisement.AdvertisementID).FirstOrDefault();
            db.Attach(objAdvertisement);
            db.ApplyPropertyChanges("Advertisements", advertisement);
            return db.SaveChanges();
        }

        public static int Insert(Advertisement advertisement)
        {
            var db = new dbADCEntities();
            db.AddToAdvertisements(advertisement);
            db.SaveChanges();
            return advertisement.AdvertisementID;
        }
    }
}
